package com.uncp.comisiondeadmision.carnet.controlador;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.uncp.comisiondeadmision.carnet.beans.CarnetBean;
import com.uncp.comisiondeadmision.carnet.beans.CarnetPostuMixBean;
import com.uncp.comisiondeadmision.carnet.service.CarnetService;
import com.uncp.comisiondeadmision.comprobantedepago.beans.PostulanteBean;



/**
 * Servlet implementation class CarnetServlet
 */
public class CarnetServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	//aka instanciamos la clase CarnetServicio para acceder
	//a todos sus metodos

	CarnetService carnetServicio = new CarnetService();
	
	
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


		String metodo = request.getParameter("metodo");
		
		if (metodo.equals("consulPosActivo")) {
			consulPosActivo(request, response);
		}
		else if(metodo.equals("listarXCodPos")){
			 listarXCodPos(request, response);
		}
		else if(metodo.equals("generarCarnet")){
			generarCarnet(request, response);
		}
		else if (metodo.equals("obtenerImagen")) {
			obtenerImagen(request, response);
		}
		else if (metodo.equals("consulPosGenerado")) {
			consulPosGenerado(request, response);
		}
		else if (metodo.equals("verCarnetGenerado")) {
			verCarnetGenerado(request, response);
		}
		else if (metodo.equals("obtenerImgVer")) {
			obtenerImgVer(request, response);
		}

	}
	
	 protected void consulPosGenerado(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		  ArrayList<CarnetPostuMixBean> array = null;
			
		  try {	
			  array = carnetServicio.getlistaPostuConCarnet();
			  		 
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		
		request.setAttribute("POS_GENERADO", array);
		request.getRequestDispatcher("/listarCarnetRegistrado.jsp").forward(request, response);	
		
	}
	
  protected void consulPosActivo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		ArrayList<PostulanteBean> array = null;
			
	  try {	
		  array = carnetServicio.getConsulPostu();
		  		  
	} catch (Exception e) {
		System.out.println(e.getMessage());
	}
	
	request.setAttribute("listaCliente", array);
	request.getRequestDispatcher("/carnetRegistrado.jsp").forward(request, response);	
	}
	
  protected void listarXCodPos(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

	      
                PostulanteBean bean = null; 
                String codigo = request.getParameter("codigo");
	            System.out.println("listarXCodPos ->"+codigo);
	            String pendiente = "Pendiente";
		try {
			
			bean = carnetServicio.getConBusPos(Integer.parseInt(codigo),pendiente);
				
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		
		request.setAttribute("BEAN",bean);
		
		request.getRequestDispatcher("/carnetRegistrado.jsp").forward(request, response);
			
	}
  
    protected void generarCarnet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
    	
    	String cod = request.getParameter("codPos");
        System.out.println("Codigo Alumno :"+cod);
        String estado = "Generado";
        int codigoGenerado = 0;
    	try {
    	
    	carnetServicio.getActuCarnet(Integer.parseInt(cod));

    	CarnetBean carnet = new CarnetBean();
      //  carnet.setIntCodCarnet(codigoGenerado);
    	carnet.setStrEstaCarnet(estado);
    	carnet.setIntCodPostu(Integer.parseInt(cod));
    	
    	carnetServicio.getRegistrarCarnet(carnet);
        
    	codigoGenerado = carnetServicio.getCodigoGenerado();
    	
    	request.setAttribute("GENERADO",codigoGenerado);  	
        	
		} catch (Exception e) {
			System.out.println("se cayo!!"+e.getMessage());
			request.setAttribute("GENERADO",codigoGenerado);  
		}
    	
	request.getRequestDispatcher("/carnetRegistrado.jsp").forward(request, response);
    	
    }
    protected void obtenerImagen(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
   
    	response.setContentType("image/jpg");
    	String pendiente = "Pendiente";
    	
        PostulanteBean bean = null; 
        String codigo = request.getParameter("cod");
        System.out.println("obtenerImagen ->"+codigo);
         
         try {
	   
	       bean = carnetServicio.getConBusPos(Integer.parseInt(codigo),pendiente);


	       

            } catch (Exception e) {
	               System.out.println(e.getMessage());
         }
                 //para pintar la imagen de la BD 
                ServletOutputStream out = response.getOutputStream();
                out.write(bean.getBlobImagen());
       	
    }
    
    protected void verCarnetGenerado(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
    	
    	
    	String codigo = request.getParameter("codigo");
    	System.out.println("verCarnetGenerado :"+codigo);
    	PostulanteBean bean = null; 
    	String Activo = "Activo";
    	try {
			
    		bean = carnetServicio.getConBusPos(Integer.parseInt(codigo), Activo);
    		
		} catch (Exception e) {
			 System.out.println(e.getMessage());
		}
   // 	request.setAttribute("codPostulante", bean.getintCodAlumno());
   // 	request.setAttribute("apePaterno",bean.getStrApePater());
   // 	request.setAttribute("apeMaterno", bean.getStrApeMater());
  //  	request.setAttribute("nomPostulante", bean.getStrNombre());
    	//para pintar la imagen de la BD 
		
	     
	        
        request.setAttribute("VER_CARNET",bean);  
        getServletContext().getRequestDispatcher("/carnetGeneradoVer.jsp").forward(request, response);
    }
    
    protected void obtenerImgVer(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
    
    	response.setContentType("image/jpg");
    	String activo = "Activo";
    	
        PostulanteBean bean = null; 
        String codigo = request.getParameter("codigo");
        System.out.println("obtenerImagen ->"+codigo);
         
         try {
	   
	       bean = carnetServicio.getConBusPos(Integer.parseInt(codigo),activo);

            } catch (Exception e) {
	               System.out.println(e.getMessage());
         }
                 //para pintar la imagen de la BD 
                ServletOutputStream out = response.getOutputStream();
                out.write(bean.getBlobImagen());
    }

}
